package ossFileinfo

import (
	"common/comCopier"
	"common/dao/model"
	comJwt "common/jwt"
	"common/mysql"
	"context"
	"file/internal/svc"
	"file/internal/types"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type GetFileListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetFileListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileListLogic {
	return &GetFileListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetFileListLogic) GetFileList(req *types.ListParmas) (resp *types.ListData, err error) {
	// todo: add your logic here and delete this line
	user, err := comJwt.GetUser(l.ctx)
	fmt.Println(user)
	if err != nil {
		return nil, err
	}
	params := mysql.QueryList{
		Page:    int(req.Page),
		Size:    int(req.PageSize),
		Key:     "name",
		FindKey: req.Keyword,
		DB:      l.svcCtx.DB,
	}
	list, i, err := mysql.GetListCustom[model.SysFile](&params, func(db *gorm.DB) {
		//db.Where(`user_id =?`, user.UUID)
		if req.GroupName != "" {
			db.Where(`group_name =?`, req.GroupName)
		}

	})
	//list, i, err := mysql.GETList[model.SysFile](&params)
	if err != nil {
		return nil, err
	}
	var (
		rests = types.ListData{}
	)
	err = comCopier.CopyWithOption(&rests.List, list)
	if err != nil {
		return nil, err
	}
	rests.Total = i
	return &rests, nil
}
